package com.zxl.always.on.display.ui.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zxl.always.on.display.R;
import com.zxl.always.on.display.model.PaletteSet;

/**
 * Created by Administrator on 2018/3/5.
 */

public class BackgroundSelectView extends RecyclerView {

    private int mSelectPosition;

    private ClockAdapter mClockAdapter;

    private TabGroupView.OnItemClickListener mOnItemClickListener;

    public BackgroundSelectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setLayoutManager(new LinearLayoutManager(context, HORIZONTAL, false));
        setHasFixedSize(true);
        setAdapter(mClockAdapter = new ClockAdapter());
    }

    public void setOnItemClickListener(TabGroupView.OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public int getSelectPosition() {
        return mSelectPosition;
    }

    public void setSelectPosition(int mSelectPosition) {
        this.mSelectPosition = mSelectPosition;
    }

    class ClockAdapter extends Adapter<ClockViewHolder> {

        @Override
        public ClockViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ClockViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.widget_settings_clock_bg_item, parent, false));
        }

        @Override
        public void onBindViewHolder(ClockViewHolder holder, int position) {
            holder.bind(position);
        }

        @Override
        public int getItemCount() {
            return PaletteSet.BG_PREVIEW_RESOURCE_ARRAYS.length;
        }
    }

    class ClockViewHolder extends ViewHolder implements OnClickListener {

        View itemView;

        private int position;

        private ImageView srcView;

        private View selectView;

        private TextView mTitleView;

        public ClockViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            itemView.setOnClickListener(this);
            srcView = itemView.findViewById(R.id.clockstyle_preview_image);
            selectView = itemView.findViewById(R.id.clockstyle_selected_image);
            mTitleView = itemView.findViewById(R.id.clockstyle_none_text);
        }

        public void bind(int position) {
            this.position = position;
            int resourceArray = PaletteSet.BG_PREVIEW_RESOURCE_ARRAYS[position];
            if (resourceArray == -1) {
                mTitleView.setVisibility(View.VISIBLE);
                srcView.setVisibility(View.GONE);
            } else {
                srcView.setVisibility(View.VISIBLE);
                mTitleView.setVisibility(View.GONE);
                srcView.setImageResource(resourceArray);
            }
            selectView.setVisibility(mSelectPosition == position ? VISIBLE : GONE);
        }

        @Override
        public void onClick(View v) {
            if (null != mOnItemClickListener) {
                mSelectPosition = position;
                mClockAdapter.notifyDataSetChanged();
                mOnItemClickListener.onItemClick(position);
            }
        }
    }
}
